/*	____________________________________________________________________________

	                Parser Component for the PhiMP Compiler
				<SEE PhiMP Compiler Grammar FOR LATEST GRAMMAR>
	____________________________________________________________________________  */

#ifndef PARSER
#define PARSER

#include "code.h"

class Parser
{
public:

	Parser();
	// Initializes the parser object.

	void SystemGoal();
	// Checks <system goal> syntax and semantics.

	void SyntaxError(string additionalMsg = "");
	// Processes a syntax error and aborts compilation.

	void Warning(string additionalMsg);
	// A warning will popup but compilation will continue

private:

	Token savedToken;
	bool tokenAvailable;


/* _____________________________________________________________________________ */

	Token NextToken();
	// Gets the next token and saves it.

	void Match(Token t);
	// Returns successfully if the next token matches t; otherwise, a syntax 
	// error occurs and compilation is aborted.
/* _____________________________________________________________________________ */


	void Program();     
	// Checks <program> syntax and semantics.

	void IdList();
	// Checks <id_list> syntax and semantics.

	void Ident(ExprRec& result);
	// Attempts to match an identifier and process it.

	void VarList();
	// Check <var_list> syntax and semantics

	void DeclarationList();
	// Check <declaration_list> syntax and semantics

	void DeclareParam();
	// Check <declare_param> syntax and semantics

	void DataType(ExprRec& result);
	// Check <data_type> syntax and semantics

	bool InitVar(ExprRec& result);
	// Check <init_var> syntax and semantics

	void InitArray(ExprRec& result);
	// Check <init_array> syntax and semantics

	void Function();
	// Check <function> syntax and semantics

	void ParamList();
	// Check <param_list> syntax and semantics

	void Return(ExprRec& result);
	// Check <return> syntax and semantics

	void ExprList(bool isWrite = false);
	// Checks <expr_list> syntax and semantics.

	void Expression(ExprRec& result);
	// Checks <expression> syntax and semantics.

	void Factor(ExprRec& result);
	// Checks <factor> syntax and semantics

	void Term(ExprRec& result);
	// Checks <term> syntax and semantics (exponentiation)

	void Primary(ExprRec& result);
	// Checks <primary> syntax and semantics.

	void Literal(ExprRec& result);
	// Check <literal> syntax and semantics

	void BoolExpr(ExprRec& result, bool isIf, ExprRec& nextAddr);
	// Check <bool_expr> syntax and semantics

	void Relation(ExprRec& result, bool isLoop, ExprRec& nextAddr);
	// Check <relation> syntax and semantics

	void BoolVarOrArray(ExprRec& result);
	// Check <bool_var_or_array> syntax and semantics

	void VarOrArray(ExprRec& result);
	// Check <var_or_array> syntax and semantics

	bool AddOp(OpRec& op);
	// Attempts to match an addition or subtraction operator and process it.

	bool MultOp(OpRec& op);
	// Attempts to match a multiplication, division, or mod operator and process it

	bool UnaryOp();
	// Attempts to match a unary operator and process it

	bool LogOp(OpRec& op);
	// Attempts to match an and or or operator and process it

	bool RelOp(OpRec& op);
	// Attempts to match a relational operator and process it

	void Statement(int forCount);
	// Checks <statement> syntax and semantics.

	void StatementList(int forCount);
	// Checks <statement list> syntax and semantics.

	void IfStatement();
	// Check <if_statement> syntax and semantics

	void ElifStatement(ExprRec& elifAddr, bool isIf, ExprRec& nextAddr);
	// Check <elif_statement> syntax and semantics

	void ElseStatement(ExprRec& elifAddr);
	// Check <else_statement> syntax and semantics

	void ForLoop(int forCount);
	// Check <for_loop> syntax and semantics

	void ForPrimary(ExprRec &result);
	// Check <for_primary_loop> syntax and semantics

	void WhileLoop();
	// Check <while_loop> syntax and semantics

	void ReadStatement();
	// Check <read_statement> syntax and semantics

	void WriteStatement();
	// Check <write_statement> syntax and semantics

	void WritelnStatement();
	// Check <writeln_statement> syntax and semantics

};

#endif